package cn.kaiux.bigdata.homework.week02;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author kaiux@foxmail.com
 * @date 2022/5/2 14:57
 */
public class PhoneTrafficStatistics {

    public static class PhoneTrafficStatisticsMapper extends Mapper<LongWritable, Text, Text, PhoneTrafficDTO> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, PhoneTrafficDTO>.Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] values = line.split("\t");
            // 过滤脏数据
            if (11 != values.length) return;
            String phone = values[0].trim();
            int up = 0, down = 0;
            if (StringUtils.isNumeric(values[8].trim())) {
                up = Integer.parseInt(values[8].trim());
            }
            if (StringUtils.isNumeric(values[9].trim())) {
                down = Integer.parseInt(values[9].trim());
            }
            context.write(new Text(phone), PhoneTrafficDTO.builder().up(up).down(down).build());
        }
    }

    public static class PhoneTrafficStatisticsReducer extends Reducer<Text, PhoneTrafficDTO, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<PhoneTrafficDTO> values, Reducer<Text, PhoneTrafficDTO, Text, Text>.Context context) throws IOException, InterruptedException {
            int up = 0, down = 0;
            for (PhoneTrafficDTO value : values) {
                up += value.getUp();
                down += value.getDown();
            }
            int total = up + down;
            String result = up + "\t" + down + "\t" + total;
            context.write(key, new Text(result));
        }
    }

    public static class PhoneTrafficStatisticsCombiner extends Reducer<Text, PhoneTrafficDTO, Text, PhoneTrafficDTO> {
        @Override
        protected void reduce(Text key, Iterable<PhoneTrafficDTO> values, Reducer<Text, PhoneTrafficDTO, Text, PhoneTrafficDTO>.Context context) throws IOException, InterruptedException {
            int up = 0, down = 0;
            for (PhoneTrafficDTO value : values) {
                up += value.getUp();
                down += value.getDown();
            }
            context.write(key, PhoneTrafficDTO.builder().up(up).down(down).build());
        }

    }

    public static class PhoneTrafficStatisticsDriver {
        public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
            if (2 != args.length) {
                System.err.println("Please set args: <input path> <output path>");
                System.exit(-1);
            }
            Configuration configuration = new Configuration();
            Job job = Job.getInstance(configuration);
            job.setJarByClass(PhoneTrafficStatisticsDriver.class);
            job.setJobName("Phone Traffic Statistics");

            FileInputFormat.addInputPath(job, new Path(args[0]));
            FileOutputFormat.setOutputPath(job, new Path(args[1]));

            job.setMapperClass(PhoneTrafficStatisticsMapper.class);
            job.setReducerClass(PhoneTrafficStatisticsReducer.class);
            job.setCombinerClass(PhoneTrafficStatisticsCombiner.class);

            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(PhoneTrafficDTO.class);

            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);

            System.exit(job.waitForCompletion(true) ? 0 : 1);
        }
    }

}